﻿using System;
using System.Windows;

namespace DotNetImage.Imaging.Filters
{
    public class Moire : FilterBase
    {
        private Point[,] points = null;
        private Point mid = new Point();

        [ParameterAttribute(Name = "Degree", DefaultValue = 0.05)]
        public double Degree { get; set; }


        protected override void PrepareData(DotNetImage bmp)
        {
            bmp.CreatePixelCopy();
            points = new Point[nWidth, nHeight];
            mid.X = nWidth / 2;
            mid.Y = nHeight / 2;
        }

        protected override void ProcessPixel(ref Pixel pix, uint x, uint y)
        {
            uint trueX = x - (uint)mid.X;
            uint trueY = y - (uint)mid.Y;
            double theta = Math.Atan2((trueX), (trueY));
            double radius = Math.Sqrt(trueX * trueX + trueY * trueY);
 
            int newX = (int)(radius * Math.Sin(theta + Degree * radius));
            points[x, y].X = (newX > 0 && newX < nWidth) ? newX : 0;

            int newY = (int)(radius * Math.Sin(theta + Degree * radius));
            points[x, y].Y = (newY > 0 && newY < nHeight) ? newY : 0;
        }

        protected override void PostProcessing(DotNetImage bmp)
        {
            base.PostProcessing(bmp);
            OffsetFilterAbs filter = new OffsetFilterAbs();
            filter.Offset = points;
            filter.Apply(bmp);
        }
    }
}
